package com.print;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import mycoffee.order.model.Discount;
import mycoffee.order.model.Order;
import mycoffee.order.model.OrdersDetail;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import com.citizen.jpos.command.ESCPOS;
import com.citizen.jpos.printer.CMPPrint;
import com.citizen.jpos.printer.ESCPOSPrinter;

public class ESCPSample2 {
	private ESCPOSPrinter posPtr;
	private final char ESC = ESCPOS.ESC;
	private final char LF = ESCPOS.LF;
	private Order mOrder;
	private Discount mDiscount;

	public ESCPSample2(Order order, Discount discount) {
		posPtr = new ESCPOSPrinter("UTF-8");
		mOrder = order;
		mDiscount = discount;
	}

	public ESCPSample2(Order order) {
		posPtr = new ESCPOSPrinter("UTF-8");
		mOrder = order;
	}

	public ESCPSample2() {
		posPtr = new ESCPOSPrinter("UTF-8");
	}

	String getWrapString( String content, int maxLength ) {
		String spaceName = "               ";
		int contentLength = content.length();
		if (contentLength <= maxLength) {
			return (content + spaceName).substring(0, maxLength);
		}
		
		// Find the last index of space, < maxLength.
		int spaceIndex = content.lastIndexOf(' ', maxLength - 1);
		if (spaceIndex < 0) {
			spaceIndex = maxLength;
		}
		
		return content.substring(0, spaceIndex) + LF + this.getWrapString(content.substring(spaceIndex + 1), maxLength);
	}

	public void receipt() throws UnsupportedEncodingException {		
		// Get today's date			 
		Date date = new Date();
		Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = formatter.format(date);
		
		File sdcard = Environment.getExternalStorageDirectory();
		// Get the text file
		Uri path = Uri.parse("android.resource://mycoffee.order/raw/logo_web");
		File file = new File(sdcard, "logo_web.bmp");
		try {
			posPtr.printBitmap("android.resource://mycoffee.order/raw/logo_web",
					CMPPrint.CMP_ALIGNMENT_CENTER);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}		
		posPtr.printText("Ton Duc Thang Tp. Phan Thiet",
				CMPPrint.CMP_ALIGNMENT_CENTER, 0,
				0);
		posPtr.printNormal(" " + LF);
		posPtr.printText("DT: 062 3 821 379",
				CMPPrint.CMP_ALIGNMENT_CENTER, 0,
				0);
		posPtr.printNormal(" " + LF);
		posPtr.printNormal(" " + LF);
		posPtr.printText("Hoa Don",
				CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_FNT_BOLD,
				CMPPrint.CMP_TXT_2HEIGHT|CMPPrint.CMP_TXT_2WIDTH);
		posPtr.printNormal(" " + LF);
		posPtr.printText(" ",
				CMPPrint.CMP_ALIGNMENT_LEFT, 0,
				0);
		posPtr.printNormal(" " + LF);
		if (mOrder != null) {
			//String spaceName = "               ";
			String spaceAmount = "  ";
			String spacePrice = "      ";
			String spaceTotalPrice = "       ";
			String space = "                        ";
			ArrayList<OrdersDetail> mOrderDetails = mOrder.getOrdersDetails();
			posPtr.printNormal("Nguoi tao: admin" + LF);
			posPtr.printNormal("Khu vuc: " + mOrder.getAreaName() + LF);
			posPtr.printNormal("Ban: " + String.valueOf(mOrder.getTable()) + LF);	
			posPtr.printNormal("Ngay: " + strDate + LF);	
			posPtr.printNormal(ESC + "|lA" + ESC + "|bC" + "................" + LF);
			
			String foodName= "";
			for (OrdersDetail od : mOrderDetails) {		
				double sotien;
				if (od.getAmount() == 0 || od.getFood().getPrice() == 0){
					sotien = 0;
				} else {
					sotien = (Double.valueOf(od.getAmount()) * od.getFood().getPrice());				
				}

				foodName = this.getWrapString(od.getFood().getName_vi(), 15);
				posPtr.printNormal(ESC + "|lA" + foodName
						+ (spaceAmount + String.valueOf(od.getAmount())).substring(String.valueOf(od.getAmount()).length()) + (spacePrice + String.valueOf(od.getFood().getPrice()).replace(".0", "")).substring(String.valueOf(od.getFood().getPrice()).replace(".0", "").length()-1)
						+ (spaceTotalPrice + String.valueOf(sotien).replace(".0", "")).substring(String.valueOf(sotien).replace(".0", "").length()-1) + LF);
			}
			posPtr.printNormal(ESC + "|lA" + ESC + "|bC" + "................" + LF);
			posPtr.printNormal(ESC + "|lA" + "Tong: " + (space + String.valueOf(mOrder.getTotal()).replace(".0", "") + "d").substring(String.valueOf(mOrder.getTotal()).replace(".0", "").length())
					+ LF);
			if (mOrder.getDiscount() != null){
				posPtr.printNormal(ESC + "|lA" + "Giam gia: " + (space + String.valueOf(mOrder.getDiscount().getTotalDiscountMoney(mOrder.getTotal())).replace(".0", "") + "d").substring(String.valueOf(mOrder.getDiscount().getTotalDiscountMoney(mOrder.getTotal())).replace(".0", "").length()+4)
						+ LF);
			}
			posPtr.printText("Tong Cong",
					CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_FNT_BOLD,
					CMPPrint.CMP_TXT_2HEIGHT|CMPPrint.CMP_TXT_2HEIGHT);
			posPtr.printNormal(" " + LF);
			posPtr.printText("**" + String.valueOf(mOrder.getTotalMoneyWillPay()) + "**",
					CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_FNT_BOLD,
					CMPPrint.CMP_TXT_2HEIGHT|CMPPrint.CMP_TXT_2HEIGHT);
			posPtr.printNormal(" " + LF);
			posPtr.printText("CAM ON QUY KHACH",
					CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_FNT_DEFAULT,
					CMPPrint.CMP_TXT_1HEIGHT|CMPPrint.CMP_TXT_1HEIGHT);
			posPtr.printNormal(" " + LF);
			posPtr.printText("HEN GAP LAI",
					CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_FNT_DEFAULT,
					CMPPrint.CMP_TXT_1HEIGHT|CMPPrint.CMP_TXT_1HEIGHT);
			posPtr.printNormal(" " + LF);
			
		}
		posPtr.printNormal(" " + LF);
		posPtr.printNormal(" " + LF);
		posPtr.printNormal(" " + LF);
	}

	public void sample1() throws UnsupportedEncodingException {
		receipt();
		posPtr.printNormal("" + LF);
		posPtr.printNormal("" + LF);
		posPtr.printNormal("" + LF);
	}	

	public void printOrder() throws UnsupportedEncodingException {
		boolean hasDrink = false;
		boolean hasFood = false;
		
		// Check if has drink or food.
		ArrayList<OrdersDetail> mOrderDetails = mOrder.getOrdersDetails();
		for (OrdersDetail od : mOrderDetails) {
			if (hasDrink && hasFood) {
				break;
			}
			
			if (hasDrink == false && od.getFood().isDrink()) {
				hasDrink = true;
			}

			if (hasFood == false && od.getFood().isFood()) {
				hasFood = true;
			}
		}
		
		if (hasDrink) {
			this.printDrink();
		}
		
		if (hasFood) {
			this.printFood();
		}
	}

	private void printFood() throws UnsupportedEncodingException {
		// Get today's date			 
		Date date = new Date();
		Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = formatter.format(date);
		
		posPtr.printText("Thuc An",
				CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_FNT_BOLD,
				CMPPrint.CMP_TXT_2HEIGHT|CMPPrint.CMP_TXT_2WIDTH);
		posPtr.printNormal(" " + LF);
		posPtr.printText(" ", CMPPrint.CMP_ALIGNMENT_LEFT, 0, 0);
		posPtr.printNormal(" " + LF);
		if (mOrder != null) {		
			String spaceName = "                   ";
			String spaceAmount = "  ";
			ArrayList<OrdersDetail> mOrderDetails = mOrder.getOrdersDetails();
			posPtr.printNormal("Nguoi tao: admin" + LF);
			// new [ngoc]: need transfer area number.
			posPtr.printNormal("Khu vuc: " + String.valueOf(mOrder.getAreaName()) + LF);	
			posPtr.printNormal("Ban: " + String.valueOf(mOrder.getTable()) + LF);	
			posPtr.printNormal("Ngay: " + strDate + LF);	
			posPtr.printNormal(ESC + "|lA" + ESC + "|bC" + "................" + LF);
			String foodName = "";
			for (OrdersDetail od : mOrderDetails) {
				if (od.getFood().isFood()) {
					foodName = this.getWrapString(od.getFood().getName_vi(), 15);
					posPtr.printNormal(ESC + "|lA" + foodName
							+ (spaceAmount + String.valueOf(od.getAmount())).substring(String.valueOf(od.getAmount()).length()) + LF);
				}
			}
			
			posPtr.printNormal(ESC + "|lA" + ESC + "|bC" + "................" + LF);
			posPtr.printNormal(" " + LF);
			
		}
	}

	private void printDrink() throws UnsupportedEncodingException {		
		// Get today's date			 
		Date date = new Date();
		Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = formatter.format(date);
		
		posPtr.printText("Thuc Uong",
				CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_FNT_BOLD,
				CMPPrint.CMP_TXT_2HEIGHT|CMPPrint.CMP_TXT_2WIDTH);
		posPtr.printNormal(" " + LF);
		posPtr.printText(" ", CMPPrint.CMP_ALIGNMENT_LEFT, 0, 0);
		posPtr.printNormal(" " + LF);
		if (mOrder != null) {		
			String spaceName = "                   ";
			String spaceAmount = "  ";
			ArrayList<OrdersDetail> mOrderDetails = mOrder.getOrdersDetails();
			posPtr.printNormal("Nguoi tao: admin" + LF);
			// new [ngoc]: need transfer area number.
			posPtr.printNormal("Khu vuc: " + String.valueOf(mOrder.getAreaName()) + LF);	
			posPtr.printNormal("Ban: " + String.valueOf(mOrder.getTable()) + LF);	
			posPtr.printNormal("Ngay: " + strDate + LF);	
			posPtr.printNormal(ESC + "|lA" + ESC + "|bC" + "................" + LF);
			String foodName = "";
			for (OrdersDetail od : mOrderDetails) {
				if (od.getFood().isDrink()) {
					foodName = this.getWrapString(od.getFood().getName_vi(), 15);
					posPtr.printNormal(ESC + "|lA" + foodName
							+ (spaceAmount + String.valueOf(od.getAmount())).substring(String.valueOf(od.getAmount()).length()) + LF);
				}
			}
			
			posPtr.printNormal(ESC + "|lA" + ESC + "|bC" + "................" + LF);
			posPtr.printNormal(" " + LF);
			
		}
	}
}
